//
//  CollectViewController.m
//  SuperNews
//
//  Created by qiang on 16/1/18.
//  Copyright © 2016年 qiang. All rights reserved.
//

#import "CollectViewController.h"
#import "CoreDataManager.h"
#import "Collect.h"
#import "YZQWebViewViewController.h"

@interface CollectViewController () <UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, strong) UITableView *myTableView;
@property (nonatomic, strong) CoreDataManager *coreDataManager;
@property (nonatomic, strong) NSMutableArray *collectArr;
@end

@implementation CollectViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationItem.title = @"我的收藏";
    self.coreDataManager = [CoreDataManager shareCoreDataManager];
    self.collectArr = [NSMutableArray array];
    [self createSubviews];
    [self selectCollectData];
}

- (void)createSubviews
{
    self.navigationController.navigationBar.tintColor = [UIColor colorWithRed:0.989 green:0.943 blue:1.000 alpha:1.000];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemReply target:self action:@selector(leftAction:)];
    self.navigationItem.rightBarButtonItem = self.editButtonItem;
    self.myTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height) style:UITableViewStylePlain];
    [self.view addSubview:self.myTableView];
    self.myTableView.delegate = self;
    self.myTableView.dataSource = self;
    self.myTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.myTableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
}

- (void)leftAction:(UIButton *)sender
{
    [self dismissViewControllerAnimated:YES completion:^{
    }];
}

- (void)selectCollectData
{
    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"Collect" inManagedObjectContext:self.coreDataManager.managedObjectContext];
    [fetchRequest setEntity:entity];
    NSError *error = nil;
    NSArray *fetchedObjects = [self.coreDataManager.managedObjectContext executeFetchRequest:fetchRequest error:&error];
    if (fetchedObjects == nil) {
        NSLog(@"error%@", error);
    }
    [self.collectArr setArray:fetchedObjects];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.collectArr.count;
    
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIndex = @"cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIndex];
    Collect *coll = [self.collectArr objectAtIndex:indexPath.row];
    cell.textLabel.text = coll.title;
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    YZQWebViewViewController *detail = [[YZQWebViewViewController alloc] init];
    Collect *coll = [self.collectArr objectAtIndex:indexPath.row];
    detail.docId = coll.docId;
    [self.navigationController pushViewController:detail animated:YES];
}

// 编辑
- (void)setEditing:(BOOL)editing animated:(BOOL)animated
{
    [super setEditing:editing animated:animated];
    [self.myTableView setEditing:editing animated:animated];
}
// 设置删除按钮文字
- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return @"确认删除?";
}

// 实现逻辑编辑
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    // 删除数据
    Collect *coll = [self.collectArr objectAtIndex:indexPath.row];
    // 删除数据库中的内容
    [self.coreDataManager.managedObjectContext deleteObject:coll];
    [self.coreDataManager saveContext];
    [self.collectArr removeObjectAtIndex:indexPath.row];
    //重新刷新一下数据
    [tableView reloadData];
    if (0 == self.collectArr.count) {
        
        UIImageView *back = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
        back.image = [UIImage imageNamed:@"nocol"];
        [self.view addSubview:back];
        
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(120, 500, 250, 40)];
        label.text = @"没有任何收藏内容";
        label.font = [UIFont boldSystemFontOfSize:24.0];
        label.textColor = [UIColor blackColor];
        [self.view addSubview:label];
    }
}

// 移动
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath{
    Collect *coll = [self.collectArr objectAtIndex:sourceIndexPath.row];
    [self.collectArr removeObjectAtIndex:sourceIndexPath.row];
    [self.collectArr insertObject:coll atIndex:destinationIndexPath.row];
    
}
// 设置跨区不能移动
- (NSIndexPath *)tableView:(UITableView *)tableView targetIndexPathForMoveFromRowAtIndexPath:(NSIndexPath *)sourceIndexPath toProposedIndexPath:(NSIndexPath *)proposedDestinationIndexPath
{
    if (sourceIndexPath.section == proposedDestinationIndexPath.section){
        return proposedDestinationIndexPath;
    }
    return sourceIndexPath;
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
